package com.woniuxy.meal.controller;


import com.woniuxy.meal.common.Result;
import com.woniuxy.meal.form.deleteCartForm;
import com.woniuxy.meal.service.dto.SubmitCartDto;
import com.woniuxy.meal.exception.parameterValidateException.ParameterValidateException;
import com.woniuxy.meal.exception.parameterValidateException.ParameterValidateExceptionCode;
import com.woniuxy.meal.form.AddCartForm;
import com.woniuxy.meal.form.SubmitCartForm;
import com.woniuxy.meal.form.UpdateCartForm;
import com.woniuxy.meal.service.UserCartService;
import com.woniuxy.meal.service.dto.UserCartDto;
import com.woniuxy.meal.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户购物车表 前端控制器
 * </p>
 *
 * @author zz
 * @since 2023年06月16日
 */
@RestController
@RequestMapping("/userCart")
@Api(tags ="购物车相关业务")
public class UserCartController {

    @Autowired
    private UserCartService userCartService;
    @Autowired
    private RedisUtil redisUtil;



    @ApiOperation("购物车新增操作")
    @PostMapping("/addCart")
    public Result addCart(@RequestBody @Valid AddCartForm form,BindingResult br,HttpServletRequest request) throws Exception {
        if (br.hasErrors()){
            throw new ParameterValidateException(ParameterValidateExceptionCode.CODE_IS_WRONG);
        }
        Long userId = redisUtil.getUserIdByToken(request.getHeader("token"));
        userCartService.addCart(form, userId);
        return Result.success(200,"新增购物车成功",null);
    }

    @ApiOperation("更新购物车")
    @PostMapping("/updateCart")
    public Result updateCart(@Valid  @RequestBody UpdateCartForm form, HttpServletRequest request) throws Exception {
        Long userId = redisUtil.getUserIdByToken(request.getHeader("token"));
        userCartService.updateCart(form,userId);
        return Result.success(200,"更新购物车成功",null);
    }

    @ApiOperation("删除购物车")
    @PostMapping("/deleteCart")
    public Result deleteCart(@RequestBody deleteCartForm form, HttpServletRequest request) throws Exception{
//        System.out.println("idid"+form.getFoodIds());
        Long userId = redisUtil.getUserIdByToken(request.getHeader("token"));
        userCartService.deleteCart(form.getFoodIds(),userId);
        return Result.success(200,"删除购物车成功",null);
    }

    @ApiOperation("展示当前登录用户所有购物车信息")
    @GetMapping("/showCart")
    public Result showCart(HttpServletRequest request) throws Exception {
        Long userId = redisUtil.getUserIdByToken(request.getHeader("token"));
        List<UserCartDto> list = userCartService.showCart(userId);
        return Result.success(200,"展示购物车信息成功",list);
    }

    @ApiOperation("展示当前登录用户某商铺的购物车信息")
    @GetMapping("/showCartByStoreId")
    public Result showCartByStoreId(Long storeId,HttpServletRequest request) throws Exception {
        System.out.println(storeId);
        Long userId = redisUtil.getUserIdByToken(request.getHeader("token"));
        UserCartDto dto = userCartService.showCartByStoreId(userId,storeId);
        return Result.success(200,"展示购物车信息成功",dto);
    }

    @ApiOperation("提交购物车，准备生成订单接口")
    @PostMapping("/submitCart")
    public  Result submitCart(@RequestBody SubmitCartForm submitCartForm,HttpServletRequest request) throws Exception{
        System.out.println(submitCartForm);
        Long userId = redisUtil.getUserIdByToken(request.getHeader("token"));
        SubmitCartDto submitCartDto = userCartService.submitCart(submitCartForm,userId);
        return Result.success(200,"提交购物车信息成功",submitCartDto);
    }

}

